from datetime import datetime

from sqlalchemy import desc

from info import constants, db
from info.models import User, News
from info.utils.common import user_login_data
from info.utils.response_code import RET
from . import index_blue
from flask import render_template, current_app, session, jsonify, request, g

# GET请求
# cid : 分类ID
# page : 当前页码
# per_page: 每页的数量
# 全部选填
''' ajax动态加载数据,前端触发事件，事件发送请求获取数据！数据进行响应！
    动态加载数据，每当触发一个条件就会触发get请求
 '''

'''主页面显示！'''



@index_blue.route('/')
def index():
    user = User.query.get(1)
    all_news = News.query.all()
    item_to = []
    for item in all_news:
        item_to.append(item.to_dict())
    data = {
        'user': user.to_dict(),
        'item': item_to,
    }
    return render_template('blog/index.html', data=data)


'''二级页面跳转！'''


@index_blue.route('/<int:news_id>')
def get_news_detail(news_id):
    user = User.query.get(1)
    content = News.query.get(news_id)

    data = {
        'user': user.to_dict(),
        'content': content.to_dict(),
    }
    return render_template('blog/detial.html', data=data)


'''内容页！'''


@index_blue.route('/info')
def cotent():
    user = User.query.get(1)
    all_news = News.query.all()
    item_to = []
    for item in all_news:
        item_to.append(item.to_dict())
    data = {
        'user': user.to_dict(),
        'item': item_to,
    }
    return render_template('blog/info.html', data=data)


'''时间轴！'''


@index_blue.route('/time')
def time():
    user = User.query.get(1)
    all_news = News.query.order_by(News.create_time.desc()).all()
    item_to = []
    for item in all_news:
        # 实现进行转化！
        item_tos = item.to_dict()
        item_tos['create_time'] = item_tos['create_time'].split(' ')[0]
        item_to.append(item_tos)
    data = {
        'item': item_to,
        'user': user,
    }
    return render_template('blog/time.html', data=data)


'''学无止境!'''


@index_blue.route('/study')
def study():
    user = User.query.get(1)
    all_news = News.query.all()
    item_to = []
    for item in all_news:
        item_to.append(item.to_dict())
    data = {
        'user': user.to_dict(),
        'item': item_to,
    }
    return render_template('blog/list.html', data=data)


'''留言'''


@index_blue.route('/say')
def say():
    user = User.query.get(1)
    all_news = News.query.all()
    item_to = []
    for item in all_news:
        item_to.append(item.to_dict())
    data = {
        'user': user.to_dict(),
        'item': item_to,
    }
    return render_template('blog/gbook.html', data=data)


'''慢生活'''


@index_blue.route('/sllow')
def sllow():
    user = User.query.get(1)
    all_news = News.query.all()
    item_to = []
    for item in all_news:
        item_to.append(item.to_dict())
    data = {
        'user': user.to_dict(),
        'item': item_to,
    }
    return render_template('blog/life.html', data=data)


'''模板分享'''


@index_blue.route('/share')
def share():
    try:
        user = User.query.get(1)
    except Exception as e:
        return jsonify(errno=RET.DATAERR, errmsg="未查询到数据")
    all_news = News.query.all()
    item_to = []
    for item in all_news:
        item_to.append(item.to_dict())
    data = {
        'user': user.to_dict(),
        'item': item_to,
    }
    return render_template('blog/share.html', data=data)


'''新闻增加注册'''


@index_blue.route('/register')
def register():
    user = User.query.get(1)
    all_news = News.query.all()
    item_to = []
    for item in all_news:
        item_to.append(item.to_dict())
    data = {
        'user': user.to_dict(),
        'item': item_to,
    }
    return render_template('blog/register.html', data=data)


'''新闻发布验证！'''


@index_blue.route('/user/news_release', methods=['post'])
def news_release():
    title = request.form.get("title")
    pwd = request.form.get("pwd")
    digest = request.form.get("digest")
    content = request.form.get("content")
    if not all(['title', 'pwd', 'digest', 'content']):
        return jsonify(errno=RET.NODATA, errmsg="以下选项都要进行输入！")

    user = User.query.filter_by(id=1).first()
    user.password = 'xu792433'
    if not user.check_passowrd(pwd):
        return jsonify(errno=RET.PARAMERR, errmsg="您没有权限！")
    # 验证通过进行存储!
    new = News()
    new.title = title
    new.digest = digest
    new.content = content
    new.create_time = datetime.now()
    new.update_time = datetime.now()
    db.session.add(new)
    db.session.commit()
    # 取出新插入的id的值,并且修改index_image_url属性！
    url_id = News.query.order_by(desc(News.id)).first().id
    new = News.query.filter_by(id=url_id).first()
    new.index_image_url = '/' + str(url_id)
    db.session.add(new)
    db.session.commit()
    return jsonify(errno=RET.OK, errmsg="数据保存成功！")
